import random as rd
import re
#61252201 36115073
def judgeNotNull(obj):
    if obj=="" or obj=="\n":
        return False
    return True
def addAddr(accountNumber):
    while True:
        state=input("请输入所在国家")
        if judgeNotNull(state.strip()):
            break
        print("不允许为空")
    while True:
        province=input("请输入所在省份")
        if judgeNotNull(province.strip()):
            break
        print("不允许为空")
    while True:
        street=input("请输入所在街道")
        if judgeNotNull(street.strip()):
            break
        print("不允许为空")
    while True:
        doorNumber=input("请输入所在门牌号")
        if judgeNotNull(doorNumber.strip()):
            break
        print("不允许为空")
    arr=[accountNumber,state,province,street,doorNumber]
    addrs.append(arr)
def checkUser(accountNumber,bank):
    for user in users:
        if user[0]==accountNumber and user[4]==bank:
            return 2
def checkUserAccountNumber(accountNumber):
    for user in users:
        if user[0]==accountNumber :
            return True
    return False
def checkUserSize():
    if len(users)>100:
        return 3
def is_six_digit_password(password):
    pattern = r'^[0-9]{6}$'
    return bool(re.match(pattern, password))
def addUser():
    while True:
        accountNumber=rd.randint(10000000,100000000)
        if checkUserAccountNumber(accountNumber)==False:
            break
    while True:
        name=input("请输入姓名")
        if judgeNotNull(name.strip()):
            break
        print("不允许为空")

    while True:
        password=input("请输入6位数字的密码")
        if is_six_digit_password(password):
            break
    addAddr(accountNumber)
    money=0
    index=1
    for bank in banks:
        print("%s.%s"%(index,bank))
        index+=1
    while True:
        bankChoice=eval(input("请选择开户行"))
        if bankChoice>=0 and bankChoice<=len(banks):
            break
    bank=banks[bankChoice-1]
    result=checkUser(accountNumber,bank)
    if result==2:
        return result
    result=checkUserSize()
    if result==3:
        return result
    arr=[accountNumber,name,password,money,bank]
    users.append(arr)
    # print(arr)
    printAccount(accountNumber)
    return 1
def saveMoney():
    while True:
        accountNumber=eval(input("请输入账号"))
        if judgeNotNull(accountNumber):
            break
        print("不允许为空")
    while True:
        money = eval(input("请输入存钱金额"))
        if judgeNotNull(money):
            break
        print("不允许为空")
    if checkUserAccountNumber(accountNumber):
        for user in users:
            if user[0]==accountNumber:
                user[3]+=money
                return True
    return False
def checkMoney(bankBalance,money):
    if bankBalance>=money:
        return True
    return False
def checkAccountNumberAndPassowrd(accountNumber,password):
    for user in users:
        if user[0] == accountNumber and user[2]==password:
                return True
    return False
def spend(user,money):
    user[3] -= money
def checkAccountAndSpend(accountNumber,password,money):
    if checkUserAccountNumber(accountNumber)==False:
        return 1
    if checkAccountNumberAndPassowrd(accountNumber,password)==False:
        return 2
    for user in users:
        if user[0] == accountNumber and user[2]==password:
            if checkMoney(user[3],money):
                spend(user,money)
                return 66
            else:
                return 3
def Transfer(accountNumberOut,accountNumberIn,money):
    cnt=0
    for user in users:
        if user[0] == accountNumberOut:
            acount1=user
            cnt+=1
        if user[0] == accountNumberIn:
            acount2=user
            cnt+=1
        if cnt==2:
            break
    acount1[3]-=money
    acount2[3] += money

def checkAccountAndTransfer(accountNumber,accountNumberIn,password,money):
    if checkUserAccountNumber(accountNumber) == False:
        return 1
    if checkAccountNumberAndPassowrd(accountNumber, password) == False:
        return 2
    for user in users:
        if user[0] == accountNumber and user[2] == password:
            if checkMoney(user[3], money):
                Transfer(accountNumber,accountNumberIn,money)
                return 0
            else:
                return 3
def spendMoney():
    while True:
        accountNumber = eval(input("请输入账号"))
        if judgeNotNull(accountNumber):
            break
        print("不允许为空")
    while True:
        password = input("请输入密码")
        if judgeNotNull(password.strip()):
            break
        print("不允许为空")
    while True:
        money = eval(input("请输入取钱金额"))
        if judgeNotNull(money):
            break
        print("不允许为空")
    if checkUserAccountNumber(accountNumber)==False:
        return 1
    return checkAccountAndSpend(accountNumber,password,money)
def transferAccounts():
    while True:
        accountNumberOut = eval(input("请输入转出账号"))
        if judgeNotNull(accountNumberOut):
            break
        print("不允许为空")
    while True:
        accountNumberIn = eval(input("请输入转入账号"))
        if judgeNotNull(accountNumberIn):
            break
        print("不允许为空")
    while True:
        password = input("请输入转出密码")
        if judgeNotNull(password.strip()):
            break
        print("不允许为空")
    while True:
        money = eval(input("请输入转出金额"))
        if judgeNotNull(money):
            break
        print("不允许为空")
    if checkUserAccountNumber(accountNumberOut)==False or checkUserAccountNumber(accountNumberIn)==False:
        return 1
    return checkAccountAndTransfer(accountNumberOut,accountNumberIn,password,money)
def printAccount(accountNumber):
    printHeader=["账号","姓名","密码","余额","开户行","所在国家","所在省份","所在街道","所在门牌号"]
    index = 0
    for user in users:
        if user[0]==accountNumber :
            for i in user:
                print("%s.%s : %s"%(index+1,printHeader[index],i))
                index+=1
            break
    for addr in addrs:
        if addr[0] == accountNumber:
            for i in addr:
                if i ==accountNumber:
                    continue
                print("%s.%s : %s" % (index,printHeader[index], i))
                index += 1
            break
def searchAccount():
    while True:
        accountNumber = eval(input("请输入账号"))
        if judgeNotNull(accountNumber):
            break
        print("不允许为空")
    while True:
        password = input("请输入密码")
        if judgeNotNull(password.strip()):
            break
        print("不允许为空")
    if checkUserAccountNumber(accountNumber) == False:
        print("1")
        return 1
    if checkAccountNumberAndPassowrd(accountNumber, password) == False:
        print("2")
        return 2
    printAccount(accountNumber)
    return 3
if __name__ == '__main__':
    users=[]
    addrs=[]
    banks=["中国工商银行的昌平支行","中国工商银行的海淀支行"]
    while True:
        print("中国工商银行账户管理系统")
        choices=["开户","存钱","取钱","转账","查询","Bye"]
        index=1
        for i in choices:
            print("*%s.%s"%(index,i))
            index+=1
        while True:
            choice = eval(input("请输入操作序号[1-6]"))
            if judgeNotNull(choice):
                break
            print("请输入选项数字")
        if choice==1:
            result=addUser()
            if result==1:
                print("用户添加成功,请牢记账号密码")
            elif result==2:
                print("用户已经存在")
            elif result==3:
                print("用户已满")
        elif choice==2:
            if saveMoney():
                print("存钱成功")
            else:
                print("用户库里没有该用户")
        elif choice==3:
            result=spendMoney()
            if result==1:
                print("账号不存在")
            elif result==2:
                print("密码不对")
            elif result==3:
                print("钱不够")
            else:
                print("取钱成功")
        elif choice==4:
            result=transferAccounts()
            if result == 1:
                print("账号不存在")
            elif result == 2:
                print("密码不对")
            elif result == 3:
                print("钱不够")
            else:
                print("转账成功")
        elif choice==5:
            result=searchAccount()
            if result==1:
                print("该用户不存在")
            elif result==2:
                print("密码不正确")
            elif result==3:
                print("查询成功")
        elif choice == 6:
            break
        else:
            print("请重新选择")
        enterContinue=input("按任意键继续...")